<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="账单号" prop="billNo">
      <el-input v-model="dataForm.billNo" placeholder="账单号"></el-input>
    </el-form-item>
    <el-form-item label="商户订单编号" prop="merOrderId">
      <el-input v-model="dataForm.merOrderId" placeholder="商户订单编号"></el-input>
    </el-form-item>
    <el-form-item label="支付通知id" prop="notifyId">
      <el-input v-model="dataForm.notifyId" placeholder="支付通知id"></el-input>
    </el-form-item>
    <el-form-item label="银联商户id" prop="mid">
      <el-input v-model="dataForm.mid" placeholder="银联商户id"></el-input>
    </el-form-item>
    <el-form-item label="发送报文" prop="sendMsg">
      <el-input v-model="dataForm.sendMsg" placeholder="发送报文"></el-input>
    </el-form-item>
    <el-form-item label="结果报文" prop="resultMsg">
      <el-input v-model="dataForm.resultMsg" placeholder="结果报文"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          billNo: '',
          merOrderId: '',
          notifyId: '',
          mid: '',
          sendMsg: '',
          resultMsg: '',
          state: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          billNo: [
            { required: true, message: '账单号不能为空', trigger: 'blur' }
          ],
          merOrderId: [
            { required: true, message: '商户订单编号不能为空', trigger: 'blur' }
          ],
          notifyId: [
            { required: true, message: '支付通知id不能为空', trigger: 'blur' }
          ],
          mid: [
            { required: true, message: '银联商户id不能为空', trigger: 'blur' }
          ],
          sendMsg: [
            { required: true, message: '发送报文不能为空', trigger: 'blur' }
          ],
          resultMsg: [
            { required: true, message: '结果报文不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/collection/uploaddatainfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.billNo = data.uploadDataInfo.billNo
                this.dataForm.merOrderId = data.uploadDataInfo.merOrderId
                this.dataForm.notifyId = data.uploadDataInfo.notifyId
                this.dataForm.mid = data.uploadDataInfo.mid
                this.dataForm.sendMsg = data.uploadDataInfo.sendMsg
                this.dataForm.resultMsg = data.uploadDataInfo.resultMsg
                this.dataForm.state = data.uploadDataInfo.state
                this.dataForm.createTime = data.uploadDataInfo.createTime
                this.dataForm.updateTime = data.uploadDataInfo.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/collection/uploaddatainfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'billNo': this.dataForm.billNo,
                'merOrderId': this.dataForm.merOrderId,
                'notifyId': this.dataForm.notifyId,
                'mid': this.dataForm.mid,
                'sendMsg': this.dataForm.sendMsg,
                'resultMsg': this.dataForm.resultMsg,
                'state': this.dataForm.state,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
